package com.geekstudy.week4.proxy;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class DynamicEnhancer {

    private String name;

    private Object target;

    private Class interfaceClass;

    private InvocationHandler invocationHandler;

    private Object proxy;

    private List<Annotation> annotationList;

    private Boolean demoBulkHead;

    private int demoBulkHeadExecuteTimes;

    public DynamicEnhancer(String name, Object target, Class interfaceClass, InvocationHandler invocationHandler) {
        this.name = name;
        this.target = target;
        this.interfaceClass = interfaceClass;
        this.invocationHandler = invocationHandler;

        Object proxy = Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class<?>[]{interfaceClass}, invocationHandler);
        this.proxy = proxy;

        annotationList = new ArrayList<Annotation>();
        Annotation[] annotationArray = interfaceClass.getAnnotations();
        for(Annotation annotation : annotationArray) {
            annotationList.add(annotation);
            if("com.geekstudy.week4.proxy.DemoBulkHead".equals(annotation.annotationType().getName())) {
                demoBulkHead = true;
                DemoBulkHead demoBulkHead = (DemoBulkHead)annotation;
                demoBulkHeadExecuteTimes = demoBulkHead.executeTime();
            }
        }

    }

    public String getName() {
        return name;
    }

    public Object getTarget() {
        return target;
    }

    public Class getInterfaceClass() {
        return interfaceClass;
    }

    public InvocationHandler getInvocationHandler() {
        return invocationHandler;
    }

    public Object getProxy() {
        return proxy;
    }

    public List<Annotation> getAnnotationList() {
        return annotationList;
    }

    public Boolean getDemoBulkHead() {
        return demoBulkHead;
    }

    public int getDemoBulkHeadExecuteTimes() {
        return demoBulkHeadExecuteTimes;
    }
}
